package com.hangwei.aicabinet;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hangwei.aicabinet.adapter.AdminAdapter;
import com.hangwei.aicabinet.adapter.DoorColorAdapter;
import com.hangwei.aicabinet.model.entry.AdminCloseAllDoor;
import com.hangwei.aicabinet.model.entry.AdminLoopComplete;
import com.hangwei.aicabinet.model.entry.AdminOpenSuccess;
import com.hangwei.aicabinet.model.entry.ColseDoorFinish;
import com.hangwei.aicabinet.net.ViewCallBack;
import com.hangwei.aicabinet.presenter.LogoutPresenter;
import com.hangwei.aicabinet.util.LoadingDialog;
import com.hangwei.aicabinet.util.SPKey;
import com.hangwei.aicabinet.util.SPUtil;
import com.hangwei.aicabinet.util.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * Created by 张龙臣 on 2018/10/31.
 */

public class AdminActivity extends BaseActivity {

    private android.widget.LinearLayout llLogout;
    private LogoutPresenter mPresenter;
    private android.support.v7.widget.RecyclerView recyclerView;
    private AdminAdapter mAdapter;
    private Dialog dialog;
    private DoorColorAdapter mDoorColorAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);
        this.recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        this.llLogout = (LinearLayout) findViewById(R.id.llLogout);
        RecyclerView rvDoorColor = findViewById(R.id.rvDoorColor);
        EventBus.getDefault().register(this);
        App.isAdmin = true;
        mPresenter = new LogoutPresenter(null);

        SPUtil.setInt(SPKey.Admin.OPEN_DOOR, -1);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        mAdapter = new AdminAdapter();


        recyclerView.setAdapter(mAdapter);
        llLogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
            }
        });

        rvDoorColor.setLayoutManager(new GridLayoutManager(this, 2));
        mDoorColorAdapter = new DoorColorAdapter();
        rvDoorColor.setAdapter(mDoorColorAdapter);
        refreshDoorView();
    }

    private void refreshDoorView() {
        mPresenter.getAdminDoorView(mDoorColorAdapter);
    }

    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_check, null);
        TextView tvConfirm = inflate.findViewById(R.id.tvConfirm);
        TextView tvCancel = inflate.findViewById(R.id.tvCancel);
        TextView tvMsg = inflate.findViewById(R.id.tvMsg);
        TextView tvTitle = inflate.findViewById(R.id.tvTitle);
//        tvTitle.setText("提示");
        tvMsg.setText("是否确定退出登录？");
        tvCancel.setText("取消");
        tvConfirm.setText("确定");
        builder.setView(inflate);
        builder.setCancelable(true);
        final AlertDialog dialog = builder.create();
        dialog.show();
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
                logout();
            }
        });
    }

    Handler handler = new Handler();

    private void logout() {
        final Dialog dialog1 = LoadingDialog.showDialog(AdminActivity.this, "退出登录中");
        mPresenter.logout(new ViewCallBack() {
            @Override
            public void onNetworkStart() {
                dialog1.show();
            }

            @Override
            public void onNetworkComplete(boolean isRight) {
                if (dialog1.isShowing())
                    dialog1.dismiss();
                handler.removeCallbacksAndMessages(null);
                if (isRight) {
                    SPUtil.setString(SPKey.Account.PERSON_ID, "");
                    ToastUtil.showToast("已退出登录");
                    Intent intent10 = new Intent(AdminActivity.this, LoginActivity.class);
                    intent10.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent10);
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAdminOpenSuccess(AdminOpenSuccess event) {
        //开锁成功，记录到SP哪个锁打开了，刷新界面
        SPUtil.setInt(SPKey.Admin.OPEN_DOOR, App.tempLock);
        mAdapter.notifyDataSetChanged();
        mPresenter.updateLock(new ViewCallBack() {
            @Override
            public void onNetworkStart() {

            }

            @Override
            public void onNetworkComplete(boolean isRight) {

            }
        },1);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAdminCloseAllDoor(AdminCloseAllDoor event) {
        //此时已经在轮询
        View inflate = LayoutInflater.from(this).inflate(R.layout.dialog_close_door, null);
        dialog = new Dialog(this, R.style.generic_dialog);
//        dialog.setContentView(inflate);
//        dialog.setCancelable(false);// 不可以用“返回键”取消
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(inflate, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.show();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAdminLoopComplete(AdminLoopComplete event){
        if (dialog!=null&&dialog.isShowing())
            dialog.dismiss();
        //更新锁状态
        mPresenter.updateLock(new ViewCallBack() {
            @Override
            public void onNetworkStart() {

            }

            @Override
            public void onNetworkComplete(boolean isRight) {
                if (isRight){
                    //上传标签
//                    mPresenter.closeDoorForAdmin();
                    mPresenter.toolsOnlineAndOutline();
                }
            }
        },0);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onColseDoorFinish(ColseDoorFinish event) {
        mAdapter.notifyDataSetChanged();
        refreshDoorView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
